home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: giuliano@ix.netcom.com(Giuliano Carlini)
- Newsgroups: comp.lang.c++
- Subject: Why don't you use garbage collection
- Date: 11 Apr 1996 06:58:08 GMT
- Organization: Netcom
- Message-ID: <4kiai0$mjd@dfw-ixnews8.ix.netcom.com>
- NNTP-Posting-Host: lbx-ca7-14.ix.netcom.com
- X-NETCOM-Date: Thu Apr 11 1:58:08 AM CDT 1996
-
- I'm a long time proponent of using garbage collection in C and C++
- programs, and I'm curious:
- - How many others are there?
- - Why don't most C/C++ programmers use it?
- I'm particularly interested in finding out why most C/C++ don't use it.
- While I have my own theories - which I'll describe below - I'm
- interested
- in finding out more directly from those who are against it. I've spent
- a
- good part of the past 4 years trying to convince people to use it, and
- I've got a handle on why the small group of people I associate with are
- not using it, but I'd like to find out more about why the C/C++
- community
- as a whole does not.
-
- I hope that with this information, I'll be able to perfect a more
- convincing presentation for why the default should be to use garbage
- collection, and for why explicit calls to free/delete should be used
- only
- in the rare cases for which garbage collection is inappropriate.
-
- What follows is my belief for why garbage collection is so little used.
- Feel free to respond to anything I say below, but please, first respond
- to the questions above. I believe that most people don't use garbage
- collection because either they:
- - don't know what it is
- - don't know it can be used with C/C++.
- - are misinformation
- - are biased against it by the C/C++ culture
- In my experience, most C/C++ programmers either don't know what garbage
- collection is, or don't know that it can be used with C/C++. After all,
- no major C/C++ compiler includes a garbage collector. At least, as far
- as I know. I hope I'm wrong, and that someone can correct me. But even
- after, I tell them what it is, and that it can be used with C++, almost
- everyone still rejects it.
-
- At first, most offer technical reasons for rejecting it. Almost all are
- based on misinformation, since garbage collection is usable and
- benificial
- for the vast majority of systems. The most often mentioned is that
- garbage
- collection is too inefficient. Either it uses too much CPU, or too much
- memory. Fortunately, this is easy to rebut. The research literature
- gives
- ample evidence that modern garbage collectors for C/C++ use around the
- same CPU time as programs that explicitly call delete/free. My personal
- experience is that typical programs that call delete/free use about the
- same amount of memory as garbage collected programs. In order to impose
- the predictability needed to permit calls to delete, they are forced to
- do deep copies of complex data structures. This uses a great deal of
- memory. Garbage collected programs need use only a single copy of the
- data, and this compensates for the additional memory taken up by
- unreachable objects.
-
- Another reason often given is that garbage collection introduces long
- pause times that are acceptable to users. This was certainly true of
- garbage collectors at one time, but modern collectors allow the client
- program to employ several techniques to eliminate pause times. The most
- sophisticated is to operate in a separate thread. Thus while the
- garbage
- collection thread is executing, the user can still interact with the
- system. Another technique is to run the collector when the user has
- been
- idle for X seconds. If the user has been idle so far, he is likely to
- remain idle. So, the collector runs while the user is making no demands
- on
- the system. If he happens to resume interating with the program in the
- middle of the collection, the collector detects this, and abandons the
- collection. The collector can then wait for the next idle period.
-
- Other technical reasons are offered, but I'll skip them as this note is
- already much too long. They apply only to unusual applications that are
- outside the mainstream. Even most of these reasons are wrong though.
-
- So, we're left with the cultural reasons, and these are I believe the
- "real" reasons why people reject garbage collection. I can shoot down
- incorrect technical arguments, but it's tough to dispute culture,
- although
- I sure try. The crux of cultural reason against garbage collection is
- that
- relying on a garbage collector is "sloppy". The "Code talk" note in the
- January Byte is a great example of this thinking. This sort of C/C++
- programmer believes that it is just wrong to not explicitly call
- delete.
- That if you don't, your an undisciplined hack. That if you don't,
- you've
- turned to the dark side and surely will come to a bad end.
-
- I'm not sure how to address this. My current argument seems to get them
- thinking, but isn't immediately convincing. Anyone have any ideas on a
- better approach. Or perhaps on a better presentation. My argument is
- that
- computers are very good at mechanistic, repetitive tasks. And that
- people
- are usually pretty bad at them; being mechanistic and repetitive, these
- tasks are rather boring, which means that our attention often strays.
- People
- routinely turn over such tasks to computers. For example, bookkeeping
- or
- inventory control. And what else is memory deallocation but a sort of
- bookkeeping or inventory control. It's just that rather than being for
- a resource outside of the computer it is for one inside.
-
- For anyone who may be wondering, I believe that we should use garbage
- collection because:
- - It vastly decreases the number of bugs in programs which use
- it.
- - It vastly decreases the time to complete programs which use
- it.
- - It vastly increases the understandability, reuseability,
- extensability, and maintainability of programs which use
- it.
-
- So, do you use garbage collection in your C/C++ programs, and if not,
- why not?
-
- giuliano
-
-